﻿package com.content.model.LoadedAssets
{
	import flash.display.Loader;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.text.StyleSheet;
	import flash.utils.getDefinitionByName;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.puremvc.as3.utilities.startupmanager.interfaces.IStartupProxy;
	
	public class FontProxy extends EntityProxy implements IStartupProxy
	{
		public static const NAME:String 						= "FontProxy";
        public static const SRNAME:String 						= "FontSRProxy";
		public static const LOAD_COMPLETE:String				= NAME + "-" + "LoadComplete";
		public static const LOAD_ERROR:String					= NAME + "-" + "LoadError";
		
		public function FontProxy() 
		{
			super( NAME, null );
		}
		
		public function load() :void 
		{
//            sendNotification( AppFacade.FONT_LOADING );
			
			var request:URLRequest = new URLRequest("assets/swf/font.swf");
			var loader:Loader = new Loader();
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			
			loader.load(request);
			
			data = loader;
        }
		
		private function loaderCompleteHandler(event:Event):void 
		{
			AssignFonts();
			sendLoadedNotification( LOAD_COMPLETE, NAME, SRNAME );
		}
		
		private function errorHandler(e:IOErrorEvent):void 
		{
            sendLoadedNotification( LOAD_ERROR, NAME, SRNAME );
        }
		
		private function get Asset():Loader
        {
            return data as Loader;
        }
		
		/**
		 * 加载进来的字体，以类的形式列举在此
		 * 其中 "FONT_ARIAL" 是系统本身预先嵌入的字体，在 font.swf 加载之前已经利用 Embed 标签置入
		 */
//		public static var FONT_ARIAL:String = 'Arial';
//		public static var FONT_ARIAL:String;
		public static var FONT_TAHOMA:String;
		public static var FONT_YAHEI:String;
		public static var FONT_YAHEI_BOLD:String;
		
		private function AssignFonts():void{
//			FONT_ARIAL = RegisterFonts("FONT_ARIAL");
			FONT_TAHOMA = RegisterFonts("FONT_TAHOMA");
			FONT_YAHEI = RegisterFonts("FONT_YAHEI");
			FONT_YAHEI_BOLD = RegisterFonts("FONT_YAHEI_BOLD");
		}
		
		private function RegisterFonts(fontName:String):String{
			// extract and register the font Class object
			var fontClass:Class = Asset.contentLoaderInfo.applicationDomain.getDefinition(fontName) as Class;
			Font.registerFont( fontClass );		
			
			var font:Font = new fontClass() as Font;
			// get the registered font name and return it
			return font.fontName;
		}

	}
}